package com.wang.myblog.service.impl;

import com.wang.myblog.common.PageResponse;
import com.wang.myblog.controller.common.UploadController;
import com.wang.myblog.entity.Article;
import com.wang.myblog.entity.Kind;
import com.wang.myblog.mapper.ArticleMapper;
import com.wang.myblog.mapper.KindMapper;
import com.wang.myblog.service.KindService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class KindServiceImpl implements KindService {

    @Autowired
    private KindMapper kindMapper;

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public boolean insert(Kind kind) {
        if (kindMapper.insert(kind) == 1) {
            return true;
        }
        return false;
    }

    /**
     *删除分类，删除的时候将该分类的的文章的分类置空并放入回收站
     */

    @Override
    public boolean deleteById(Integer id) {
        Kind kind=kindMapper.queryById(id);
        //System.out.println(id);
        if (kindMapper.deleteById(id) == 1) {
            UploadController.deleteCommonImg(kind.getImg());
            List<Article> list=articleMapper.queryArticleByKindId(id);
            list.forEach(article -> {
                article.setKindId(-1);
                article.setStatus(2);
                articleMapper.update(article);
            });
            return true;
        }
        return false;

    }

    @Override
    public Kind queryById(Integer id) {
        return kindMapper.queryById(id);
    }

    /**
     * @return 对象列表
     * @Description 查询全部数据
     * 分页使用MyBatis的插件实现
     */
    @Override
    public List<Kind> queryAll() {
        List<Kind> kinds= kindMapper.queryAll();
        kinds.forEach(kind -> {
            kind.setArticleCount(articleMapper.getCountByKindId(kind.getId()));
        });
        return kinds;
    }

    @Override
    public List<Kind> queryAll(Integer status) {
        List<Kind> kinds= kindMapper.queryAll();
        kinds.forEach(kind -> {
            kind.setArticleCount(articleMapper.getCountByKindIdAndStatus(kind.getId(),status));
        });
        return kinds;
    }

    /**
     * @param kind 实例对象
     * @return 对象列表
     * @Description 实体作为筛选条件查询数据
     */
    @Override
    public List<Kind> queryAll(Kind kind) {
        return kindMapper.queryAll(kind);
    }

    /**
     * @param kind 实例对象
     * @return 是否成功
     * @Description 修改数据，哪个属性不为空就修改哪个属性
     */
    @Override
    public boolean update(Kind kind) {
        Kind kind1=kindMapper.queryById(kind.getId());
        if (kindMapper.update(kind) == 1) {
            if(!kind.getImg().equals(kind1.getImg())) {
                UploadController.deleteCommonImg(kind1.getImg());
            }
            return true;
        }
        return false;
    }

    /**
     * @return java.util.List<com.moti.entity.Kind>
     * @Description 根据文章ID查询所有分类
     * @Author 莫提
     * @Date 20:28 2020/9/2
     * @Param [id]
     **/
    @Override
    public Kind queryByArticleId(Integer id) {
        return null;
    }

    /**
     * @return java.lang.Integer
     * @Description 根据文集ID获取关联文章的数量
     * @Param [id]
     **/
    @Override
    public Integer getArticleCount(Integer id) {
        return null;
    }

    /**
     * @return java.lang.Integer
     * @Description 获取分类的数量
     **/
    @Override
    public Integer getCount() {
        return kindMapper.getCount();
    }

    @Override
    public Kind queryByName(String name){
        return kindMapper.queryByName(name);
    }

    @Override
    public PageResponse queryByPageAndSearchMsg(Integer draw, Integer start, Integer limit, String searchMsg){
        List<Kind> kindList=kindMapper.queryByPageAndSearchMsg(start,limit,searchMsg);
        kindList.forEach(kind->{
            kind.setArticleCount(articleMapper.getCountByKindId(kind.getId()));
        });
        Integer total= kindMapper.getCountBySearchMsg(searchMsg);
        return new PageResponse(draw,total,total,kindList);

    }

    @Override
    public String getKindNameList() {
        String[] nameList=new String[kindMapper.getCount()];
        List<Kind> kinds= kindMapper.queryAll();
        for(int i=0;i<kinds.size();i++){
            nameList[i]=kinds.get(i).getName();
        }
        String result="[";
        for (int k=0;k < nameList.length;k++){
            result += '"'+nameList[k]+'"'+",";
        }
        return result+"]";
    }

    @Override
    public String getKindArticleReadCount() {
        Integer[] counts=new Integer[kindMapper.getCount()];
        List<Kind> kinds= kindMapper.queryAll();
        for(int i=0;i<kinds.size();i++){
            List<Article> articles=articleMapper.queryArticleByKindId(kinds.get(i).getId());
            Integer a=0;
            for (Article article:articles){
                a +=article.getReadCount();
            }
            counts[i]=a;
        }
        return Arrays.toString(counts);
    }


}
